import minimist from "minimist";
import { fileURLToPath } from "node:url";
import { dirname, resolve } from "node:path";
import { createRequire } from "node:module";
import esbuild from "esbuild";
// 解析参数
const args = minimist(process.argv.slice(2));
// 当前文件路径
const __filename = fileURLToPath(import.meta.url);
// 当前目录
const __dirname = dirname(__filename);


const require = createRequire(import.meta.url);
// 目标模块
const target = args._[0] || "reactivity";
// 格式
const format = args.f || "esm";
const pkg = require(`../packages/${target}/package.json`);


// 入口文件
const entry = resolve(__dirname,`../packages/${target}/src/index.ts`);
// 输出文件
const outfile = resolve(
  __dirname,
  `../packages/${target}/dist/${target}-${format}.js`
);

esbuild
  .context({
    entryPoints: [entry],
    outfile,
    bundle: true,
    format,
    sourcemap: true,
    platform: "browser",
    globalName: pkg.buildOptions?.name,
  })
  .then((ctx) => {
    console.log("start dev");
    return ctx.watch();
  });
